/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view;

import com.kcp.ko.fmtnmlib.service.search.SimpleSearch;
import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Lifecycle;
import org.apache.tapestry.valid.ValidationDelegate;
import org.braintrain.domain.EvaluationBackground;
import org.braintrain.domain.EvaluationData;
import org.braintrain.domain.Module;
import org.braintrain.domain.types.KnowledgeLevel;
import org.braintrain.domain.types.TrainingRating;
import org.braintrain.services.search.SortedBackgroundsQueryFactory;

import java.util.Date;
import java.util.List;

/**
 * Tapestry Evaluation page.  Part of the navigation chain.  Appears at the end of the main
 * chain and serves as a 
 * @author John Ericksen
 *
 */

public abstract class Evaluation extends BrainTrainSecuredBasePage implements IExternalPage {
    
    public static final String ExternalName = "Evaluation";

    @Bean(lifecycle=Lifecycle.REQUEST)
    public abstract EvaluationData getData();
    
    @Bean
    public abstract ValidationDelegate getDefaultDelegate();
    
    @InjectPage("Home")
    public abstract Home getHomePage();
    
    @InjectPage("ModuleFinish")
    public abstract ModuleFinish getModuleFinishPage();
    
    public abstract KnowledgeLevel getKnowledge();
    public abstract void setKnowledge(KnowledgeLevel k);
    
    public abstract TrainingRating getRating();
    public abstract void setRating(TrainingRating rating);
    
    public abstract EvaluationBackground getBackground();
    public abstract void setBackground(EvaluationBackground b);
    
    public abstract Long getModuleId();
    public abstract void setModuleId(Long id);
    
    /**
     * External activation (see IExternalPage)
     */
    public void activateExternalPage(Object[] params, IRequestCycle cycle) {
        if(params != null && params.length > 0 && params[0] instanceof Long)
        {
            activate(cycle, (Long) params[0]);
        }
        cycle.activate(this);
    }
    
    public Module getModule()
    {
        if(getModuleId() != null)
        {
            return (Module)getGenericService().getById(Module.class, getModuleId());
        }
        return null;
    }
    
    /**
     * Initializes the current page.
     * @param cycle
     * @param moduleId
     */
    public void activate(IRequestCycle cycle, Long moduleId)
    {
        setModuleId(moduleId);
        
        cycle.activate(this);
    }
    
    /**
     * Background value accessor / mutators
     */
    public boolean getBackgroundValue() {
        return getData().getBackground().contains(getBackground());
    }
    public void setBackgroundValue(boolean backgroundValue) {
        if(backgroundValue)
        {
            getData().getBackground().add(getBackground());
        }
    }
    
    /**
     * Returns a list of all the backgrounds in sorted order
     * @return
     */
    public List<EvaluationBackground> getBackgrounds()
    {
        return getGenericService().find(new SortedBackgroundsQueryFactory());
    }
    
    /**
     * CMStaff function of adding a background to the backgrounds list
     * 
     * @param cycle
     */
    public void addBackground(IRequestCycle cycle)
    {
        Object[] params = cycle.getListenerParameters();
        if(params != null && params.length > 0 && params[0] instanceof String)
        {
            String name = (String)params[0];
            if(getGenericService().find(SimpleSearch.simpleStringSearch(EvaluationBackground.class, "background", name)).size() == 0)
            {
                EvaluationBackground background = new EvaluationBackground();
                background.setBackground(name);
                
                getGenericService().saveOrUpdate(background);
            }
        }
    }
    
    
    /**
     * Saving function.  Contains error checking.
     * 
     * @param cycle
     */
    public void submitEvaluation(IRequestCycle cycle)
    {
        if(getData().getBackground().size() == 0)
        {
            getDefaultDelegate().record(null, "You must select at least one Responsibility in Part I");
        }
        if(getData().getKnowledgeLevel() == null)
        {
            getDefaultDelegate().record(null, "You must select at least one Responsibility in Part I");
        }
        if(getData().getExpectations() == null)
        {
            getDefaultDelegate().record(null, "You must rate 'The training met my expectations' in Part II");
        }
        if(getData().getQuality() == null)
        {
            getDefaultDelegate().record(null, "You must rate 'How would you rate the quality of information' in Part II");
        }
        if(getData().getAmount() == null)
        {
            getDefaultDelegate().record(null, "You must rate 'How would you rate the amount of information' in Part II");
        }
        if(getData().getJobHelp() == null)
        {
            getDefaultDelegate().record(null, "You must rate 'To what extent did the training help you in your job' in Part II");
        }
        if(getData().getUserFriendly() == null)
        {
            getDefaultDelegate().record(null, "You must rate 'The online navigation was easy and user friendly' in Part II");
        }
        if(getData().getReaction() == null)
        {
            getDefaultDelegate().record(null, "You must rate 'What is your overall reaction to this lesson' in Part II");
        }
        
        if(!getDefaultDelegate().getHasErrors())
        {
            
            
            
            if(getModuleId() != null)
            {
                if(getModule().isActive())
                {
                    getData().setModule(getModule());
                    getData().setEvalDate(new Date(System.currentTimeMillis()));
                    getGenericService().saveOrUpdate(getData());
                }
                getModuleFinishPage().activate(cycle, getModuleId());
            }
            else
            {
                cycle.activate(getHomePage());
            }
        }
        
    }
    
}
